package com.sq.universe.log.listener;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONConfig;
import cn.hutool.json.JSONObject;
import com.alibaba.fastjson.JSON;
import com.sq.universe.log.entity.LogCenterEntity;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileWriter;
import java.util.Date;
import java.util.Map;

@RocketMQMessageListener(topic = "system-log",consumerGroup ="${rocketmq.consumer.group}" )
@Component
@Slf4j
public class LogListener implements RocketMQListener<LogCenterEntity> {

    @Value("${universe.fileBasePath}")
    private String fileBasePath;

    @Value("${universe.logMaxRows}")
    private Integer maxRows;

    private static JSONObject config = new JSONObject();

    @Override
    public void onMessage(LogCenterEntity msg) {
        try {
            String curDate = DateUtil.format(new Date(),"yyyyMMdd");
            String logKey = curDate + "-" + msg.getSystem();
            String logPath = "";
            JSONObject logConfig;
            File logFile;
            FileWriter fileWriter ;
            // 判断当天是否已经存在日志
            if(config.getJSONObject(logKey) == null){
                logConfig = new JSONObject();
                logConfig.set("fileIndex",0);
                logConfig.set("rows",0);
                logPath = fileBasePath + File.separator + msg.getSystem() + File.separator + curDate ;
                File dir = new File(logPath);
                if(!dir.exists()){
                    dir.mkdirs();
                }
                logConfig.set("dir",logPath);
                config.set(logKey,logConfig);
            }else{
                logConfig = config.getJSONObject(logKey);
            }
            logFile = new File(logConfig.getStr("dir") + File.separator + msg.getSystem() + "-" + logConfig.getInt("fileIndex") + ".log" );
            fileWriter = new FileWriter(logFile,true);
//            // 判断日志文件是否存在
//            if(logConfig.get("logFile") == null){
//                logFile = new File(logPath + File.separator + msg.getSystem() + "-" + logConfig.getInt("fileIndex") + ".log" );
//                logConfig.set("logFile",logFile);
//            }else{
//                logFile =  logConfig.get("logFile",File.class);
//            }
//            // 判断写流是否存在
//            if(logConfig.get("fileWriter") == null){
//                fileWriter = new FileWriter(logFile);
//            }else{
//                fileWriter = logConfig.get("fileWriter",FileWriter.class);
//            }
            fileWriter.append(msg.getMsg());
            fileWriter.flush();
            logConfig.set("rows",logConfig.getInt("rows")  + 1 );
            if(logConfig.getInt("rows") > maxRows){
                logConfig.set("fileIndex",logConfig.getInt("fileIndex") + 1)  ;
                fileWriter.close();
            }
        }catch (Exception e){
            log.error("日志写入文件异常报错",e);
        }
    }
}
